package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.GeneratePageService;
import com.heima.common.exception.CustException;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.enums.AppHttpCodeEnum;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 壮哥
 * @Date 2021/10/5/13:52
 */
@Service
@Slf4j
public class GeneratePageServiceImpl implements GeneratePageService {
    @Autowired
    ApArticleMapper apArticleMapper;
    @Autowired
    Configuration configuration;
    @Autowired
    AuthorMapper authorMapper;
    @Resource(name = "minIOFileStorageService")
    FileStorageService fileStorageService;
    @Value("${file.minio.prefix}")
    private String prefix;

    @Override
    public void generateArticlePage(String content, ApArticle apArticle) {
        log.info("文章静态页生成方法被调用   文章内容:{}  ,  文章基本信息: {}",content,apArticle);
        try {
            //1.准备模板
            Template template = configuration.getTemplate("article.ftl");
            //2.主备数据
            Map data = new HashMap<>();
            data.put("article", apArticle);
            List<Map> contentList = JSONArray.parseArray(content, Map.class);
            //文章内容信息
            data.put("content", contentList);
            ApAuthor author = authorMapper.selectById(apArticle.getAuthorId());
            //作者对应apUserId
            data.put("authorApUserId", author.getUserId());
            //3.基于template替换模板   输出到指定地方
            //准备字符输出流
            StringWriter stringWriter = new StringWriter();
            template.process(data, stringWriter);
            //替换模板将替换后的内容输出到字符输出流中
            //将字符输出流中的字符串内容封装到字节数组输入流中
            InputStream inputStream=new ByteArrayInputStream(stringWriter.toString().getBytes());
            //4.将输入流所代表的静态页内容上传到minIO中

            String store = fileStorageService.store(prefix, apArticle.getId() + ".html", "text/html", inputStream);
            apArticle.setStaticUrl(store);
            apArticleMapper.updateById(apArticle);
            log.info("文章静态页生成完毕  静态资源路径:{]",store);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("获取文章详情模板错误 异常:{}",e.getMessage());
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR,"获取文章详情模板错误");
        } catch (TemplateException e) {
            e.printStackTrace();
            log.error("替换文章详情模板异常 异常:{}",e.getMessage());
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR,"替换文章详情模板异常");
        }
    }
}